package com.daqian.mall.controller;

import com.daqian.mall.entity.ModuleGoodsTag;
import com.daqian.mall.rest.RestResult;
import com.daqian.mall.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@RestController
public class TagController {

    @Autowired
    private TagService goodsService;

    /**
     * 根据id获取详情
     *
     * @param id
     * @return
     */
    @GetMapping("/base/tag/{id}")
    public RestResult getById(@PathVariable("id") Long id) {
        return RestResult.success(goodsService.getById(id));
    }

    /**
     * 获取所有标签
     *
     * @return
     */
    @GetMapping("/base/tag/all")
    public RestResult listAllTag() {
        return RestResult.success(goodsService.listAllTag());
    }

    /**
     * 新增标签
     *
     * @return
     */
    @PostMapping("/base/tag")
    public RestResult saveTag(ModuleGoodsTag entity) {
        Assert.notNull(entity.getName(), "标签名不可为空");
        goodsService.saveTag(entity);
        return RestResult.success();
    }

    /**
     * 修改标签
     *
     * @return
     */
    @PutMapping("/base/tag/{id}")
    public RestResult updateTag(@PathVariable("id") Long id, @RequestBody ModuleGoodsTag entity) {
        Assert.notNull(id, "id不可为空");
        entity.setId(id);
        goodsService.updateTag(entity);
        return RestResult.success();
    }

    /**
     * 删除单个标签
     *
     * @return
     */
    @DeleteMapping("/base/tag/{id}")
    public RestResult deleteTag(@PathVariable("id") Long id) {
        Assert.notNull(id, "id不可为空");
        goodsService.deleteTag(id);
        return RestResult.success();
    }

    /**
     * 删除多个标签
     *
     * @return
     */
    @DeleteMapping("/base/tag")
    public RestResult deleteTag(@RequestParam("ids[]") Long[] ids) {
        Assert.notNull(ids, "id不可为空");
        goodsService.deleteTag(ids);
        return RestResult.success();
    }
}
